package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum CurveShape implements EnumTrans<CompleteEntity.CurveShape, CurveShape> {
    /**
     * 默认曲线形状
     * 折线
     * 凸函数
     * 凹函数
     * 自定义曲线
     */
    CS_Default(CompleteEntity.CurveShape.CS_Default),
    Line(CompleteEntity.CurveShape.Line),
    Convex(CompleteEntity.CurveShape.Convex),
    Concave(CompleteEntity.CurveShape.Concave),
    Customize(CompleteEntity.CurveShape.Customize),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.CurveShape curveShape;

    CurveShape(CompleteEntity.CurveShape curveShape) {
        this.id = curveShape.getNumber();
        this.name = curveShape.name();
        this.curveShape = curveShape;
    }

    /**
     * @param curveShape
     * @return 对应的类型
     */
    public static CurveShape getScheduleTypeStruct(CompleteEntity.CurveShape curveShape) {
        Objects.requireNonNull(curveShape, "curveShape can not be null.");
        return Arrays.stream(CurveShape.values())
                .filter(e -> e.name.equals(curveShape.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.CurveShape toProto() {
        return this.curveShape;
    }

    @Override
    public CurveShape formProto(CompleteEntity.CurveShape curveShape) {
        return getScheduleTypeStruct(curveShape);
    }
}

